package functiiTrigonometrice;

import functiiTrigonometrice.Cos;
import operator.Cat;
import operator.Produs;
import functii.Functie;
/**
 * @author Feri
 */
/**
 *  This class is the implementation of the tg() function. <br />
 * See <A href="https://sites.google.com/site/sabloanedeproiectare/refactoring" > Design Patterns Lab </A>
 */

public class Tg extends Functie{
	/**
	 * 
	 * @param aux1 
	 * @return Returns the value of tg(aux1).
	 */
	public static double calcul(double aux1){
		return Math.tan(aux1);
	}
	/**
	 * If the left son is not null returns tg(<i>left son</i>).
	 */
	@Override
	public double calcul() {
		// TODO Auto-generated method stub
		if (fStang == null)
			System.exit(1);
		return calcul(fStang.calcul());
	}
	/**
	 * Returns the derived tg function.
	 */
	@Override
	public String derivare() {
		// TODO Auto-generated method stub
		if (getfStang() == null)
			System.exit(1);
		return Produs.concatTermens(getfStang().derivare(), Cat.concatTermens("1", 
				Produs.concatTermens(Cos.concatTermens(getfStang().toString()), Cos.concatTermens(getfStang().toString()))));
	}
	/**
	 * Returns the value of the derived tg function.
	 */
	@Override
	public double calculDerivata() {
		// TODO Auto-generated method stub
		if (getfStang() == null)
			System.exit(1);
		return Produs.Calcul(getfStang().calculDerivata(),
				Cat.Calcul(1,Produs.Calcul(Cos.calcul(getfStang().calculDerivata()), Cos.calcul(getfStang().calcul()))));
	
	}
	/**
	 * Overrides the toString method.
	 */
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		if (getfStang() == null)
			System.exit(1);
		return concatTermeni(getfStang().toString());
	}
	/**
	 * 
	 * @param aux1
	 * @return Same as concatTermeni, but it is static
	 */
	public static String concatTermens(String aux1){
		String aux;
		int nrP;
		nrP = aux1.charAt(0) == '(' ? 0 : 2;
		aux = "tg";
		if (nrP != 0) aux += "(";
		aux += aux1;
		if (nrP != 0) aux += ")";
		return aux;
	}
	/**
	 * 
	 * @param aux1 Auxiliary variable
	 * @return Concatenates "tg(" with the auxiliary variable
	 */
	public String concatTermeni(String aux1){
		String aux;
		int nrP;
		nrP = aux1.charAt(0) == '(' ? 0 : 2;
		aux = "tg";
		if (nrP != 0) aux += "(";
		aux += aux1;
		if (nrP != 0) aux += ")";
		return aux;
	}
	/**
	 * 
	 * @return Returns the calculated value
	 */
	public double calculeazaValoare() {
		return this.calcul();
	}
}
